package com.meichis.yndms.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.meichis.yndms.R;

import java.util.ArrayList;
import java.util.List;

public class AutoNextLineLinearLayout extends ViewGroup {

    private static final String TAG = "AutoNextLineLinearLayou";

    public static final float DEFAULT_SPACE = 10;
    private float mItemHorizontalSpace = DEFAULT_SPACE;
    private float mItemVerticalSpace;
    private int mSelfWidth;
    private int mItemHeight;

    private List<ImageView> mImageViewList = new ArrayList<>();

    public AutoNextLineLinearLayout(Context context) {
        this(context, null);
    }

    public AutoNextLineLinearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoNextLineLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AutoNextLineLinearLayout);
        mItemHorizontalSpace = ta.getDimension(R.styleable.AutoNextLineLinearLayout_horizontalSpace, DEFAULT_SPACE);
        mItemVerticalSpace = ta.getDimension(R.styleable.AutoNextLineLinearLayout_verticalSpace, DEFAULT_SPACE);
        ta.recycle();
    }

    public void setImageList(List<ImageView> imageList) {
        removeAllViews();
        this.mImageViewList.clear();
        this.mImageViewList.addAll(imageList);
        for (ImageView image : mImageViewList) {
            addView(image);
        }
    }

    //这个是描述所有的行
    private List<List<View>> lines = new ArrayList<>();

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getChildCount() == 0) {
            return;
        }
        List<View> line = null;
        lines.clear();
        mSelfWidth = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();

        int childCount = getChildCount();

        for (int i = 0; i < childCount; i++) {
            View itemView = getChildAt(i);
            if (itemView.getVisibility() != VISIBLE) {

                continue;
            }
            measureChild(itemView, widthMeasureSpec, heightMeasureSpec);
            if (line == null) {
                line = createNewLine(itemView);
            } else {
                if (canBeAdd(itemView, line)) {
                    line.add(itemView);
                } else {
                    line = createNewLine(itemView);
                }
            }
        }
        mItemHeight = getChildAt(0).getMeasuredHeight();

        int selfHeight = (int) (lines.size() * mItemHeight + mItemVerticalSpace * (lines.size() + 1) + 0.5f);
        Log.d(TAG, "selfHeight: " + selfHeight);
        setMeasuredDimension(mSelfWidth, selfHeight);
    }

    private boolean canBeAdd(View itemView, List<View> line) {
        int totalWidth = itemView.getMeasuredWidth();
        for (View view : line) {
            totalWidth += view.getMeasuredWidth();
        }
        totalWidth += mItemHorizontalSpace * (line.size() + 1);

        return totalWidth <= mSelfWidth;
    }

    private List<View> createNewLine(View itemView) {
        List<View> line = new ArrayList<>();
        line.add(itemView);
        lines.add(line);
        return line;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int topOffset = (int) mItemHorizontalSpace;
        for (List<View> views : lines) {
            int leftOffset = (int) mItemHorizontalSpace;
            for (View view : views) {
                view.layout(leftOffset, topOffset, leftOffset + view.getMeasuredWidth(), topOffset + view.getMeasuredHeight());

                leftOffset += view.getMeasuredWidth() + mItemHorizontalSpace;
            }
            topOffset += mItemHeight + mItemHorizontalSpace;
        }
    }
}

